package com.ttl.bc.report.purchasing;



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.DynaActionForm;

import com.ttl.common.util.CommonUtils;

public class PurchasingReportBO {

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void getMaterialsInStockReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {

		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 apCodeName   = mForm.get("apCodeName").toString();
		String	 itemCodeName = mForm.get("itemCodeName").toString();
		
	    String company = apCodeName;
	    String item    = itemCodeName;		

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    
	    apCodeName = "(mtr.apCode like '%"+apCodeName+"%' or "+
	                        " mtr.name2 like '%"+apCodeName+"%')  ";
	            
	    itemCodeName = "(mtr.itemCode like '%"+itemCodeName+"%' or "+
	                          " mtr.itemName like '%"+itemCodeName+"%') ";
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("startPOYear", startDate[2]);
		hashMap.put("endPOYear", endDate[2]);
		hashMap.put("startDateH",(df.format(sDate2).toString()));
		hashMap.put("endDateH",(df.format(eDate2).toString()));	    
		hashMap.put("apCodeName",apCodeName);
		hashMap.put("itemCodeName",itemCodeName);
		hashMap.put("company",company);
		hashMap.put("item",item);
				
		String jasperFile = "/ireport/MaterialsInStock3_v371.jasper";
		
		generateReport(jasperFile, hashMap, response, servlet);								
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void getTaxBuyReport(ActionForm form, HttpServletRequest request,
			HttpServletResponse response, ActionServlet servlet) throws Exception {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 apCodeName   = mForm.get("apCodeName").toString();
		String	 itemCodeName = mForm.get("itemCodeName").toString();
		
	    @SuppressWarnings("unused")
		String company = apCodeName;
	    @SuppressWarnings("unused")
		String item    = itemCodeName;		

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    	    
	    @SuppressWarnings("unused")
		Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1]),Integer.parseInt(startDate[0]));
	    @SuppressWarnings("unused")
		Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1]),Integer.parseInt(endDate[0]));    
	    
	    @SuppressWarnings("unused")
		DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
				
		String jasperFile = "/ireport/TaxBuy_v371.jasper";
		
		generateReport(jasperFile, hashMap, response, servlet);		
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void getPurchasingMaterialsReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 apCodeName   = mForm.get("apCodeName").toString();
		String	 itemCodeName = mForm.get("itemCodeName").toString();
		itemCodeName = CommonUtils.getConvertStringToUTF8(itemCodeName);
		
	    @SuppressWarnings("unused")
		String company = apCodeName;
//	    @SuppressWarnings("unused")
//		String item    = itemCodeName;		

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    	    
	    @SuppressWarnings("unused")
		Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    @SuppressWarnings("unused")
		Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    @SuppressWarnings("unused")
		DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("startDateH",(df.format(sDate2).toString()));
		hashMap.put("endDateH",(df.format(eDate2).toString()));	  		
		hashMap.put("itemCodeName",itemCodeName);		 
		String jasperFile = "/ireport/PurchMat8_v376.jasper";
		
		generateReport(jasperFile, hashMap, response, servlet);		
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void getDyestuffsChemicalsReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {

		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 apCodeName   = mForm.get("apCodeName").toString();
		String	 itemCodeName = mForm.get("itemCodeName").toString();
		String	 typeReport	 = mForm.get("typeReport").toString();
		
	    String company = apCodeName;
	    String item    = itemCodeName;		

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    
//	    System.out.println("sDate: "+sDate);
//	    System.out.println("eDate: "+eDate);
	    
	    apCodeName = "(i.ApCode like '%"+apCodeName+"%' or "+
	                        " a.Name2 like '%"+apCodeName+"%')  ";
	            
	    itemCodeName = "(i.ItemCode like '%"+itemCodeName+"%' or "+
	                          " i.ItemName like '%"+itemCodeName+"%') ";
	    
	    if(typeReport.equalsIgnoreCase("CHEMICALS")) {
	    	itemCodeName += " and i.ItemCode like '2-%' "; 
	    }else if(typeReport.equalsIgnoreCase("DYESTUFFS")) {
	    	itemCodeName += " and i.ItemCode like '3-%' ";
	    }else {
	    	itemCodeName += " and (i.ItemCode like '2-%' or i.ItemCode like '3-%')";
	    }
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("startPOYear", startDate[2]);
		hashMap.put("endPOYear", endDate[2]);
		hashMap.put("startDateH",(df.format(sDate2).toString()));
		hashMap.put("endDateH",(df.format(eDate2).toString()));	    
		hashMap.put("apCodeName",apCodeName);
		hashMap.put("itemCodeName",itemCodeName);
		hashMap.put("company",company);
		hashMap.put("item",item);
		hashMap.put("typeReport", typeReport);
		
		String jasperFile = "/ireport/DyeChem_v376.jasper";
		
		generateReport(jasperFile, hashMap, response, servlet);				
	}

	@SuppressWarnings("unchecked")
	private void generateReport(String jasperFile, HashMap hashMap,
			HttpServletResponse response, ActionServlet servlet) throws Exception {
		
		if(CommonUtils.runReportToPdf(response, servlet, hashMap, jasperFile)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}		
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = servlet.getServletConfig()
//						.getServletContext().getResourceAsStream(jasperFile);
//		
//		response.setContentType("application/pdf");
//		
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
//		
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();				
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public void getPO(ActionForm form, HttpServletRequest request,
			HttpServletResponse response, ActionServlet servlet) throws Exception {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String	 apCodeName   = "%"+ mForm.get("apCodeName").toString() +"%";

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];	      	    
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2]),Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    System.out.println("\n\n\nstartDate = "+mForm.get("startDate").toString()+"\n\n\n\n");
	    sDate2 =CommonUtils.convertStringToDate(sDate);
	    
	    @SuppressWarnings("unused")
		DateFormat df = new SimpleDateFormat("d MMMM yyyy");   ///DateFormat.getDateInstance(DateFormat.LONG,new Locale("en", "EN"));	    
						
		HashMap hashMap = new HashMap();
		
		hashMap.put("docDateStr",sDate2);
		hashMap.put("apCodeName",apCodeName);
						
		String jasperFile = "/ireport/ttlpo.jasper";
		
		generateReport(jasperFile, hashMap, response, servlet);
		
	}

	@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
	public void getPurchasingDeptProjectReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 departCode   = mForm.get("departCode").toString().trim();
		String	 projectCode = mForm.get("projectCode").toString().trim();	

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy",new Locale("th", "TH"));   
	//    DateFormat.getDateInstance(DateFormat.LONG,new Locale("th", "TH"));	    
						
	    String strDate = "วันที่     "+df.format(sDate2).toString()+"    ถึง    "+df.format(eDate2).toString();
	    
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("departCode",departCode+"%");
		hashMap.put("projectCode",projectCode+"%");
		hashMap.put("strDate",strDate);
		String jasperFile = "/ireport/purchasing_dept_project.jasper";
		if(departCode.length()==0) {
			jasperFile = "/ireport/purchasing_project_dept.jasper";
		}

		
		generateReport(jasperFile, hashMap, response, servlet);

	}
	
	public void getPurchasingDeptProjectSummaryReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 departCode   = mForm.get("departCode").toString().trim();
		String	 projectCode = mForm.get("projectCode").toString().trim();	

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy",new Locale("th", "TH"));   
	//    DateFormat.getDateInstance(DateFormat.LONG,new Locale("th", "TH"));	    
						
	    String strDate = "วันที่     "+df.format(sDate2).toString()+"    ถึง    "+df.format(eDate2).toString();
	    
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("departCode",departCode+"%");
		hashMap.put("projectCode",projectCode+"%");
		hashMap.put("strDate",strDate);
		
		String jasperSummaryFile = "/ireport/purchasing_dept_project_summary.jasper";
		

		generateReport(jasperSummaryFile, hashMap, response, servlet);
	}

	@SuppressWarnings("deprecation")
	public void getPurchasingDeptProjectWaitingReport(ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			ActionServlet servlet) throws Exception {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String[] startDate 	  = mForm.get("startDate").toString().split("-");
		String[] endDate 	  = mForm.get("endDate").toString().split("-");
		String	 departCode   = mForm.get("departCode").toString().trim();
		String	 projectCode = mForm.get("projectCode").toString().trim();	

	    String sDate= startDate[2]+"-"+startDate[1]+"-"+startDate[0];
	    String eDate= endDate[2]+"-"+endDate[1]+"-"+endDate[0];    
	    
	    Date sDate2= new Date(Integer.parseInt(startDate[2])-1900,Integer.parseInt(startDate[1])-1,Integer.parseInt(startDate[0]));
	    Date eDate2= new Date(Integer.parseInt(endDate[2])-1900,Integer.parseInt(endDate[1])-1,Integer.parseInt(endDate[0]));    
	    
	    
	    DateFormat df = new SimpleDateFormat("d MMMM yyyy",new Locale("th", "TH"));   
	//    DateFormat.getDateInstance(DateFormat.LONG,new Locale("th", "TH"));	    
						
	    String strDate = "วันที่     "+df.format(sDate2).toString()+"    ถึง    "+df.format(eDate2).toString();
	    
		HashMap hashMap = new HashMap();
		
		hashMap.put("startDate",sDate);
		hashMap.put("endDate",eDate);    
		hashMap.put("departCode",departCode+"%");
		hashMap.put("projectCode",projectCode+"%");
		hashMap.put("strDate",strDate);
		String jasperFile = "/ireport/purchasing_dept_project_waiting.jasper";
		if(departCode.length()==0) {
			jasperFile = "/ireport/purchasing_project_dept_waiting.jasper";
		}

		
		generateReport(jasperFile, hashMap, response, servlet);
		
	}
 	
}
